<%@page import="java.util.List,com.trw.trp.bean.ModelBean,com.trw.trp.bean.ModelDetail,com.trw.trp.bean.OrderBean,com.trw.trp.util.*"%>
<%@ include file="header.jsp"%>

<%
step=2;
OrderBean order = (OrderBean)session.getAttribute(TRPConstants.CURRENT_ORDER);
String project = order.getGeneralInfo().getProjectNumber() + " " + order.getGeneralInfo().getProject() + " " + order.getGeneralInfo().getProduct();
List<ModelBean> modelList = order.getModelMap().get(1);
%>

<table align="center" cellpadding="0" cellspacing="0" height="100%"
	width="100%" border="0">
	<tr>
		<td valign="top">
			<div id="modelDiv"/>
		</td>
	</tr>
</table>
<style>
.x-column-header-inner .x-column-header-text {
    white-space: normal;
}

.x-column-header-inner {
    line-height: normal;
    padding-top: 3px !important;
    padding-bottom: 3px !important;
    text-align: center;
    top: 20%;
}

</style>
<script type="text/javascript">
function saveData(){
	var flag;
	var formPanel = Ext.getCmp('sampleInfoPanel');
	if(formPanel){
		var found = false;
		var grid;
		var params = new Object();
		for(var i=0;i<<%=modelList.size()%>;i++){
			grid=Ext.getCmp('grid5_'+i);
			if(grid){
				found = true;
				for(var j=0;j<grid.getStore().getCount();j++){
					var record = grid.getStore().getAt(j);
					eval("params.p_"+i+"_5_"+(j+1)+"_149='"+(j+1)+"';");
					eval("params.p_"+i+"_5_"+(j+1)+"_150='"+record.data.partName+"';");
					eval("params.p_"+i+"_5_"+(j+1)+"_151='"+record.data.side+"';");
					eval("params.p_"+i+"_5_"+(j+1)+"_152='"+record.data.testTemp+"';");
					eval("params.p_"+i+"_5_"+(j+1)+"_153='"+record.data.inflatorType+"';");
				}
			}
		}
		Ext.Ajax.request({ 
			url: 'updateSampleInfo.action',
			method: 'post', 
			params: params,
			async:  false,
			success: function(result, request) { 
				var ret = Ext.decode(result.responseText);
				if(ret.state==1){
					flag=false;
				}
			}, 
			failure: function(result, request) { 
				flag=false;
			}
		});
		flag=true;
	}
	return flag;
}

Ext.onReady(function(){
var panel;

if(!panel){
	
	panel = Ext.create('Ext.panel.Panel',{
		autoHeight:true,
		bodyPadding: 0,
		id:'sampleInfoPanel',
		frame:true,
		renderTo:'modelDiv',
		autoScroll:false,
		items:[
	<%
	for(int i=0;i<modelList.size();i++){
		ModelBean model = modelList.get(i);
		if(model.getModelId()==12){%>
		{
			xtype:'grid',
			id:'grid5_'+<%=i%>,
			title: 'Internal Parts',
			loadMask: true,
			forceFit:true,
			autoScroll:true,
			enableColumnHide:false,
			columnLines:true,
			columns: [
				{xtype: 'rownumberer',header:'No.',width: 40},
				{ header: 'Part Name',  dataIndex: 'partName', width: 200,align:"center"},
				{ header: 'Side',  dataIndex: 'side', width: 100,align:"center"},
				{ header: 'Test Temperature',  dataIndex: 'testTemp', width: 200,align:"center"},
				{ header: 'Inflator Type',  dataIndex: 'inflatorType', width: 100,align:"center"}
			],
			store:new Ext.data.Store({
				autoLoad: true,
				fields:['partName','side', 'testTemp','inflatorType'],
				proxy: {
					type: 'ajax',
					url: 'getSampleInfo.action',
					extraParams:{idx:<%=i%>,sectionIdx:5},
					reader: {
						type: 'json',
						root: 'items',
						totalProperty: 'size'
					}
				},
				listeners:{
					'load':function(store,records,success,operation,opts){
						Ext.getCmp('amount'+'<%=i%>').setValue(store.getCount());
					}
				}
			}),
			tbar: [
			  { xtype: 'displayfield', fieldLabel: 'Customer',value:'<%=order.getGeneralInfo().getCustomer()%>' },
			  { xtype: 'tbseparator' },
			  { xtype: 'displayfield', fieldLabel: 'Project',value:'<%=project%>' },
			  { xtype: 'tbseparator' },
			  { xtype: 'displayfield', fieldLabel: 'Amount', id:'amount'+'<%=i%>' }
			]
		}
		
	<%	}if(i<modelList.size()-1){out.print(",");}
	}
	%>	
		],
		buttonAlign:'center',
		buttons:[{
			text:'Back',
			id:'btnBack',
			cls:'big-button',
			handler:function (){
				document.location='goSubModel.action';
			}
		},{
			text:'Next',
			cls:'big-button',
			handler:function (){
				Ext.MessageBox.confirm('Confirm', 'All the information in this page will be frozen after your confirmation! Still want to continue?', function(btn){
					if(btn=='yes'){
						Ext.getBody().mask('processing...');
						var flag = saveData();
						if(flag==false){
							Ext.MessageBox.alert('Warning', 'Save data failed, please try again.');
						}else if(flag==true){
							document.location="viewMatrix.action";
						}						
						Ext.getBody().unmask();
					}
				});		
			}
		}]
	});	

	
}


});
</script>

<%@ include file="footer.jsp"%>